//axios的基础封装  任何项目都需要的东西
import axios from 'axios'
// import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import router from '@/router'


const httpInstance = axios.create({
  baseURL: 'http://localhost:8080/admin',
  timeout: 50000
})

// axios请求拦截器
httpInstance.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `${token}`
  }
  console.log(config)
  return config
}, e => Promise.reject(e))

// axios响应式拦截器
httpInstance.interceptors.response.use(res => res.data, e => {
  // 401 token失效处理
  if (e.response && e.response.status === 401) {
    localStorage.removeItem('token')
    router.push('/login')
  }
  return Promise.reject(e)
})


export default httpInstance